home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.01 Jan 89 / Bezier Application / CurveLayer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-12  |  3.8 KB  |  248 lines  |  [TEXT/KAHL]

  1. /*
  2. **  CurveLayer.c  
  3. **
  4. **        These routines provide a layer of support between my bare-bones
  5. **        application skeleton and the Bezier curve code.  There's little
  6. **        here of interest outside of the mouse tracking and the curve
  7. **        drawing.
  8. **
  9. **  David W. Smith
  10. */
  11.  
  12. #include "QuickDraw.h"
  13. #include "MacTypes.h"
  14. #include "FontMgr.h"
  15. #include "WindowMgr.h"
  16. #include "MenuMgr.h"
  17. #include "TextEdit.h"
  18. #include "DialogMgr.h"
  19. #include "EventMgr.h"
  20. #include "DeskMgr.h"
  21. #include "FileMgr.h"
  22. #include "ToolboxUtil.h"
  23. #include "ControlMgr.h"
  24.  
  25. /*
  26.  *  Tracker objects.  Similar to MacAPP trackers, but much, much simpler.
  27.  */
  28. struct Tracker
  29. {
  30.     void    (*track)();
  31.     int        thePoint;
  32. };
  33.  
  34. static struct Tracker aTracker;
  35. static struct Tracker bTracker;
  36.  
  37. /*
  38.  *  The Bezier curve control points.
  39.  */
  40. Point                control[4] = {{144,72}, {72,144}, {216,144}, {144,216}};
  41.  
  42.  
  43.  
  44. /*
  45.  *  Draw
  46.  *
  47.  *  Called from the skeleton to update the window.  Draw the initial curve.
  48.  */
  49. Draw()
  50. {
  51.     PenMode(patXor);
  52.     
  53.     DrawTheCurve(control, true);
  54. }
  55.  
  56.  
  57. /*
  58.  *  DrawTheCurve
  59.  *
  60.  *  Draw the given Bezier curve in the current pen mode.  Draw the
  61.  *  control points if requested.
  62.  */
  63. DrawTheCurve(c, drawPoints)
  64.     Point        c[];
  65. {
  66.     if ( drawPoints )
  67.         DrawThePoints(c);
  68.  
  69.     BezierCurve(c[0], c[1], c[2], c[3]);
  70. }
  71.  
  72.  
  73. /*
  74.  *  DrawThePoints
  75.  *
  76.  *  Draw all of the control points.
  77.  */
  78. DrawThePoints(c)
  79.     Point        c[];
  80. {
  81.     int            n;
  82.     
  83.     for ( n = 0 ; n < 4 ; ++n )
  84.     {
  85.         DrawPoint(c, n);
  86.     }
  87. }
  88.  
  89.  
  90. /*
  91.  *  DrawPoint
  92.  *
  93.  *  Draw a single control point
  94.  */
  95. DrawPoint(c, n)
  96.     Point            c[];
  97.     int                n;
  98. {
  99.     PenSize(3, 3);
  100.     MoveTo(c[n].h - 1, c[n].v - 1);
  101.     LineTo(c[n].h - 1, c[n].v - 1);
  102.     PenSize(1, 1);
  103. }
  104.  
  105.  
  106. /*
  107.  *  GetTracker
  108.  *
  109.  *  Produce a tracker object
  110.  *
  111.  *  Called by the skeleton to handle mouse-down events.
  112.  *
  113.  *  If the mouse touches a control point, return a tracker for that point.
  114.  *  Otherwise, return a tracker that drags a gray rectangle.
  115.  */
  116. struct Tracker *
  117. GetTracker(point)
  118.     Point            point;
  119. {
  120.     void            TrackPoint(), TrackSelect();
  121.     int                i;
  122.  
  123.     aTracker.track = TrackPoint;
  124.  
  125.     for ( i = 0 ; i < 4 ; ++i )
  126.     {
  127.         if ( TouchPoint(control[i], point) )
  128.         {
  129.             aTracker.thePoint = i;
  130.             return (&aTracker);
  131.         }
  132.     }
  133.  
  134.     bTracker.track = TrackSelect;
  135.     return (&bTracker);
  136. }
  137.  
  138.  
  139. /*
  140.  *  TouchPoint
  141.  *
  142.  *  Do the points touch?
  143.  */
  144. #define abs(a) (a < 0 ? -(a) : (a))
  145.  
  146. TouchPoint(target, point)
  147.     Point            target;
  148.     Point            point;
  149. {
  150.     SubPt(point, &target);
  151.  
  152.     if ( abs(target.h) < 3 && abs(target.v) < 3 )
  153.         return (1);
  154.  
  155.     return (0);
  156. }
  157.  
  158.  
  159. /*
  160.  *  TrackPoint
  161.  *
  162.  *  Called while dragging a control point.
  163.  */
  164. void
  165. TrackPoint(tracker, point, phase)
  166.     struct Tracker        *tracker;
  167.     Point                point;
  168.     int                    phase;
  169. {
  170.     Point                savePoint;
  171.  
  172.     switch ( phase )
  173.     {
  174.     case 1:
  175.         /* initial click - XOR out the control point */
  176.         DrawPoint(control, tracker->thePoint);
  177.         break;
  178.  
  179.     case 2:
  180.         /* drag - undraw the original curve and draw the new one */
  181.         DrawTheCurve(control, false);
  182.         control[tracker->thePoint] = point;
  183.         DrawTheCurve(control, false);
  184.         break;
  185.  
  186.     case 3:
  187.         /* release - redraw the control point */
  188.         DrawPoint(control, tracker->thePoint);
  189.         break;
  190.     }
  191. }
  192.  
  193.  
  194. /*
  195.  *  TrackSelect
  196.  *
  197.  *  Track a gray selection rectangle
  198.  */
  199. static Point        first;
  200. static Rect            r;
  201.  
  202. void
  203. TrackSelect(tracker, point, phase)
  204.     struct Tracker        *tracker;
  205.     Point                point;
  206.     int                    phase;
  207. {
  208.     switch ( phase )
  209.     {
  210.     case 1:
  211.         PenPat(gray);
  212.         first = point;
  213.         SetupRect(&r, first, point);
  214.         FrameRect(&r);
  215.         break;
  216.  
  217.     case 2:
  218.         FrameRect(&r);
  219.         SetupRect(&r, first, point);
  220.         FrameRect(&r);
  221.         break;
  222.  
  223.     case 3:
  224.         FrameRect(&r);
  225.         PenPat(black);
  226.         break;
  227.     }
  228. }
  229.  
  230. /*
  231.  *  SetupRect
  232.  *
  233.  *  Setup the rectangle for tracking.
  234.  */
  235. #define min(x, y) (((x) < (y)) ? (x) : (y))
  236. #define max(x, y) (((x) > (y)) ? (x) : (y))
  237.  
  238. SetupRect(rect, point1, point2)
  239.     Rect            *rect;
  240.     Point            point1;
  241.     Point            point2;
  242. {
  243.     SetRect(rect,
  244.             min(point1.h, point2.h),
  245.             min(point1.v, point2.v),
  246.             max(point1.h, point2.h),
  247.             max(point1.v, point2.v));
  248. }